package com.emagic.manage.data.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;

/**
 * 获取Android系统信息工具类
 *
 * @author baoy
 * @version 1.0
 *          Create by 16/1/6 上午11:40
 */
public class AndroidUtil {


    /**
     * 判断当前网络状态是否可用
     *
     * @param context
     * @return
     */
    public static boolean isNetWorkAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) {
            return false;
        }
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni == null || !ni.isAvailable()) {
            return false;
        }
        return true;
    }

    /**
     * 获取系统版本
     *
     * @param ctx 应用上下文
     * @return current app version name
     */
    public static String getAppVersionName(Context ctx) {
        try {
            PackageInfo pi = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);
            return pi.versionName;
        } catch (PackageManager.NameNotFoundException e) {

            return "未知版本";
        }
    }

    /**
     * 获取系统版本名称
     *
     * @param ctx 应用上下文
     * @return current app version code
     */
    public static int getAppVersionCode(Context ctx) {
        try {
            PackageInfo pi = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);
            return pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            return -1;
        }
    }

    /**
     * 获取android系统版本
     * @return  android system version
     */
    public static String getAndroidVersion(){
        return Build.VERSION.RELEASE;
    }

}
